<?php
include '../conn.php';
include 'a-cookie.php';
$Year = $_SESSION["a-Year"];
$groupID = isset($_POST["groupId"]) ? $_POST["groupId"] : null;
$groupAdName = isset($_POST["groupAdName"]) ? $_POST["groupAdName"] : null;
$groupAdId = isset($_POST["groupAdId"]) ? $_POST["groupAdId"] : null;
$defenTime = isset($_POST["defenTime"]) ? $_POST["defenTime"] : null;
$defenPoint = isset($_POST["defenPoint"]) ? $_POST["defenPoint"] : null;
$actionType = isset($_POST["actionType"]) ? $_POST["actionType"] : null;
//答辩组添加
if ($actionType == "添加") {
    $sql1 = "insert into defenseteam VALUES (
    '2018','$groupID','$defenPoint','$defenTime','$groupAdName','$groupAdId'
)";
    $result = mysqli_query($conn, "select teacherpassword from teacher WHERE teacherid='$groupAdId'");
    $tpwd = $result->fetch_assoc();//获取密码
    $tpwd = $tpwd["teacherpassword"];
    $sql2 = "insert into administrator VALUES (
'$groupAdId','$tpwd',1
)";
    $ation1 = mysqli_query($conn, $sql1); //添加一条答辩组信息，到答辩表中；
    $ation2 = mysqli_query($conn, $sql2);//添加一条管理员信息，到管理员表中；

    $sArr = $_POST["s"];//读取提交的学生
    $tArr = $_POST["t"];//读取提交的老师
    for ($i = 0; $i < count($sArr); $i++)//设置学生表groupId的值
    {
        $sql = "update student set groupid='$groupID',defenAsignStated=1 WHERE studentId='$sArr[$i]'";
        if (mysqli_query($conn, $sql)) {
            echo "yes";
        } else {
            echo "Error: " . $sql . "<br>" . mysqli_error($conn);
        }

    }
    for ($i = 0; $i < count($tArr); $i++)//设置教师表groupId的值
    {
        $sql = "insert into teacherasigntable VALUES (
            $groupID,'$tArr[$i]')";
        if (mysqli_query($conn, $sql)) {
            echo "yes";
            header("Location:a-denfenAsign.php");
        } else {
            echo "Error: " . $sql . "<br>" . mysqli_error($conn);
        }

    }
}

// 修改答辩组信息
if ($actionType == "修改") {
    $sql4 = "select groupadId from defenseteam where groupId=$groupID";
    $row = mysqli_query($conn, $sql4)->fetch_array();
    $sql4 = "DELETE FROM administrator WHERE AdministratorId=$row[0]";
    $action4 = mysqli_query($conn, $sql4);

    $sql1 = "update student set groupid=null,defenasignstated=null WHERE groupId=$groupID";
    $sql2 = "delete from teacherasigntable WHERE groupId=$groupID";
    $sql3 = "DELETE FROM defenseteam WHERE groupId=$groupID";
    $action1 = mysqli_query($conn, $sql1);
    $action2 = mysqli_query($conn, $sql2);
    $action3 = mysqli_query($conn, $sql3);

    $sql1 = "insert into defenseteam VALUES (
    '2018','$groupID','$defenPoint','$defenTime','$groupAdName','$groupAdId'
)";
    $result = mysqli_query($conn, "select teacherpassword from teacher WHERE teacherid='$groupAdId'");
    $tpwd = $result->fetch_assoc();//获取密码
    $tpwd = $tpwd["teacherpassword"];
    $sql2 = "insert into administrator VALUES (
'$groupAdId','$tpwd',1
)";
    $ation1 = mysqli_query($conn, $sql1); //添加一条答辩组信息，到答辩表中；
    $ation2 = mysqli_query($conn, $sql2);//添加一条管理员信息，到管理员表中；

    $sArr = $_POST["s"];//读取提交的学生
    $tArr = $_POST["t"];//读取提交的老师
    for ($i = 0; $i < count($sArr); $i++)//设置学生表groupId的值
    {
        $sql = "update student set groupid='$groupID',defenAsignStated=1 WHERE studentId='$sArr[$i]'";
        if (mysqli_query($conn, $sql)) {
            echo "yes";
        } else {
            echo "Error: " . $sql . "<br>" . mysqli_error($conn);
        }

    }
    for ($i = 0; $i < count($tArr); $i++)//设置教师表groupId的值
    {
        $sql = "insert into teacherasigntable VALUES (
            $groupID,'$tArr[$i]')";
        if (mysqli_query($conn, $sql)) {
            echo "yes";
            header("Location:a-denfenAsign.php");
        } else {
            echo "Error: " . $sql . "<br>" . mysqli_error($conn);
        }

    }

}

//答辩组的删除
if ($actionType == "denfenDelete") {
    $sql4 = "select groupadId from defenseteam where groupId=$groupID";
    $row = mysqli_query($conn, $sql4)->fetch_array();
    $sql4 = "DELETE FROM administrator WHERE AdministratorId=$row[0]";
    $action4 = mysqli_query($conn, $sql4);

    $sql1 = "update student set groupid=null,defenasignstated=null WHERE groupId=$groupID";
    $sql2 = "delete from teacherasigntable WHERE groupId=$groupID";
    $sql3 = "DELETE FROM defenseteam WHERE groupId=$groupID";
    $action1 = mysqli_query($conn, $sql1);
    $action2 = mysqli_query($conn, $sql2);
    $action3 = mysqli_query($conn, $sql3);


    if ($action1 && $action2 && $action3 && $action4) {
        echo 1;
    } else echo 2;

}

//adName的检查是否存在
if ($actionType == "adNameCheck") {
    $adName = $_POST["adName"];
    $sql = "select * from teacher where teacherName='$adName'";
    $result = mysqli_query($conn, $sql);
    if ($result->num_rows > 0) {

        echo 1;
    } else {

        echo 0;
    }
}

//ajax 返回对应的教研室人

if ($actionType == "roomChoose") {
    $roomId = $_POST["roomId"];
    if ($roomId == '1') {
        $CurrSubject = "网设与安全";
    } elseif ($roomId == '2') {
        $CurrSubject = "多媒体与网传";
    } elseif ($roomId == '3') {
        $CurrSubject = "信息工程";
    }
    $sqlt1 = "select TeacherId from teacher where CurrSubject='$CurrSubject'";
    $sqlt2 = "select TeacherName from teacher where CurrSubject='$CurrSubject'";
    $sqls1 = "select StudentId from student WHERE Major='$CurrSubject'and defenAsignstated is NULL";
    $sqls2 = "select StudentName from student WHERE Major='$CurrSubject'and defenAsignstated is NULL ";

    $result = mysqli_query($conn, $sqlt1);
    $row1 = $result->fetch_all();

    $result = mysqli_query($conn, $sqlt2);
    $row2 = $result->fetch_all();

    $result = mysqli_query($conn, $sqls1);
    $row3 = $result->fetch_all();

    $result = mysqli_query($conn, $sqls2);
    $row4 = $result->fetch_all();

    $tsRow = array('tId' => $row1, 'sId' => $row3, 'tName' => $row2, 'sName' => $row4);
    if ($result) {
        echo json_encode($tsRow);
        // echo json_encode($row2);
    } else {
        echo "error";
    }
}

//学生的检索
if ($actionType == "spick") {
    $searchTest = $_POST["searchTest"];
    $searchTest = urldecode($searchTest);
    $sql = "select studentId from student WHERE (studentName
       LIKE '%" . $searchTest . "%'or studentId Like '%" . $searchTest . "%')and defenAsignstated is NULL";
    $result = mysqli_query($conn, $sql);
    $row1 = $result->fetch_all();
    $sql = "select studentName from student WHERE (studentName
       LIKE '%" . $searchTest . "%'or studentId Like '%" . $searchTest . "%')and defenAsignstated is NULL";
    $result = mysqli_query($conn, $sql);
    $row2 = $result->fetch_all();
    $tsRow = array('sName' => $row2, 'sId' => $row1);

    echo json_encode($tsRow);
}


//任务分配
if ($actionType == "autoAsign") {   //测试阶段先清除
    $sql = "delete from thesismark where 1";
    mysqli_query($conn, $sql);
    $sql = "delete from replymark where 1";
    mysqli_query($conn, $sql);
    $sql = "delete from dzsubject where 1";
    mysqli_query($conn, $sql);


    $sql = "select groupId from defenseteam";
    $row = mysqli_query($conn, $sql)->fetch_all();
    echo count($row);

    if (count($row) > 0) {
        for ($k = 0; $k < count($row); $k++) {
            $groupId = $row[$k][0];
            $sql = "select studentId ,teacher_TeacherId from student where groupid=$groupId";//查找学生集
            $sRow = mysqli_query($conn, $sql)->fetch_all();

            echo "<pre>";
            print_r($sRow);//学生集
            echo "<pre>";

            $sql = "select teacherId from teacher where teacherId in
        (select teacherId FROM teacherasigntable where groupId=$groupId)";//查找教师集
            $tRow = mysqli_query($conn, $sql)->fetch_all();

            echo "<pre>";
            print_r($tRow);//教师集
            echo "<pre>";

            //分配答辩评分评语操作//
            for($i=0;$i<count($sRow);$i++)
            {
                $sId=$sRow[$i][0];
                $result = mysqli_query($conn, "select projectId,projectName from project WHERE student_studentId=$sId");
                $Prow = $result->fetch_array();
                $Pid = $Prow[0];
                $Pname=$Prow[1];

                //汇总评分分配
                $sqld="insert into dzsubject VALUES (
          '',$Year,'$Pname',null,null,null,null,null,$Pid
              )" ;
                if (mysqli_query($conn, $sqld)) {
                    echo $sqld . "<br>";
                } else {
                    echo mysqli_error($conn);
                }


                for($t=0;$t<count($tRow);$t++){
                    $tId=$tRow[$t][0];
                    //答辩评分分配
                    $sqld="insert into replymark VALUES (
          '',$Year,null,null,null,null,null,$Pid,$tId
              )" ;
                    if (mysqli_query($conn, $sqld)) {
                        echo $sqld . "<br>";
                    } else {
                        echo mysqli_error($conn);
                    }
                }

            }


            //分配论文评阅操作
            for ($i = 0; $i < count($sRow); $i += count($tRow)) {
                for ($j = 0,$q=0; $j < count($tRow); $j++,$q++) {
                    if (!isset($sRow[$i + $q][1])) {
                        break;
                    }
                    $sId = $sRow[$i + $q][0];//学生ID
                    if ($sRow[$i + $q][1] == $tRow[$j][0]) {
                        //不能分配任务/(分配给下一个老师)

                        if ($j < count($tRow) - 1) {
                            echo $tRow[$j + 1][0] . "<br>";
                            $sRow[$i + $q][2] = $tRow[$j + 1][0];
                            $Tid = $tRow[$j + 1][0];//评分教师ID


                            $result = mysqli_query($conn, "select projectId from project WHERE student_studentId=$sId");
                            $Prow = $result->fetch_array();
                            $Pid = $Prow[0];
                            //插入
                            $sql1 = "insert into thesismark VALUES (
                        $Pid,$Year,null,null,null,'$Pid','$Tid'
                  )";


                        } else {
                            echo $tRow[0][0] . "<br>";
                            $sRow[$i + $q][2] = $tRow[0][0];
                            $Tid = $tRow[0][0];//评分教师ID

                            $result = mysqli_query($conn, "select projectId from project WHERE student_studentId=$sId");
                            $Prow = $result->fetch_array();
                            $Pid = $Prow[0];
                            //插入
                            $sql1 = "insert into thesismark VALUES (
                        $Pid,$Year,null,null,null,'$Pid','$Tid'
                  )";
                        }

                    } else {
                        //能分配任务/
                        echo $tRow[$j][0] . "<br>";
                        $sRow[$i + $q][2] = $tRow[$j][0];
                        $Tid = $tRow[$j][0];//评分教师ID


                        $result = mysqli_query($conn, "select projectId from project WHERE student_studentId=$sId");
                        $Prow = $result->fetch_array();
                        $Pid = $Prow[0];
                        //插入
                        $sql1 = "insert into thesismark VALUES (
                        $Pid,$Year,null,null,null,'$Pid','$Tid'
                  )";

                    }
                    if (mysqli_query($conn, $sql1)) {
                        echo $sql1 . "<br>";
                    } else {
                        echo mysqli_error($conn);
                    }
                }
            }
            echo "<pre>";
            print_r($sRow);
            echo "<pre>";

        }
    } else {
        //未分组，要清除评分关系
        $sql = "delete from thesismark where 1";
        mysqli_query($conn, $sql);
        $sql = "delete from replymark where 1";
        mysqli_query($conn, $sql);
    }

}







?>




